﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Net.NetworkInformation;
using System.Diagnostics;

namespace ee_wp_tourmaker.WebServices
{
    public class SaveChangesService
    {
        private AzureService.SyncChallenges copy;

        public void SaveChangesStart()
        {
            bool isAvailable = NetworkInterface.GetIsNetworkAvailable();
            if (isAvailable == false)
            {
                Debug.WriteLine("No connection for uploading changes");
            }
            else
            {
                var client = new ee_wp_tourmaker.AzureService.Service1Client();
                client.OpenCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_OpenCompleted);
                client.OpenAsync(client);
                client.SyncChangesCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_SyncChallengesCompleted);
            }
        }

        void client_SyncChallengesCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            App.MainModel.Changes.Items.Clear();
            ((AzureService.Service1Client)e.UserState).CloseAsync();
        }

        void client_OpenCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            var client = (ee_wp_tourmaker.AzureService.Service1Client)e.UserState;
            client.SyncChangesAsync(App.MainModel.Changes, client);
            copy = App.MainModel.Changes;
            
        }



    }
}
